package game;

import static org.lwjgl.opengl.GL11.*;
import java.util.ArrayList;
import java.util.Random;

import org.lwjgl.util.vector.Vector3f;

public class World {
	public Game game;
	private Random rand = new Random();
	private Player player;
	private Camera camera;
	private ArrayList<Entity> entities = new ArrayList<Entity>();
	private ArrayList<Box> boxes = new ArrayList<Box>();
	private FPSController controller;
	

	public World(Game game) {
		this.game = game;
		camera = new Camera();

		player = new Player(this, new Vector3f(0, 10, 0));
		controller = new FPSController(this, player);
		addEntity(player);

		build();
	}

	public void update(int delta) {
		controller.update(delta);
		camera.setPos(new Vector3f(player.getPos().x, player.getPos().y + player.height, player.getPos().z));
		camera.setRot(player.getRot());
		for (int i = 0; i < entities.size(); i++) {
			entities.get(i).update(delta);
		}
	}

	public void clear() {
		boxes.clear();
		System.out.println("World cleared");
	}

	public void build() {
		Box b = new Box(new Vector3f(-50, 0, -50), new Vector3f(100, 5, 100));
		//b.setColor(0, 1, 0);
		addBox(b);
	}

	public void render() {
		for (int i = 0; i < entities.size(); i++) {
			glLoadIdentity();
			camera.translate();
			entities.get(i).render();
		}

		for (int i = 0; i < boxes.size(); i++) {
			glLoadIdentity();
			camera.translate();
			boxes.get(i).render();
		}
	}

	public ArrayList<Box> getBoxes() {
		return boxes;
	}

	public void addEntity(Entity e) {
		entities.add(e);
	}

	public void addBox(Box b) {
		boxes.add(b);
	}
}
